package com.xmlit.project.engine.struct.impl;

//import java.util.regex.Pattern;

import com.xmlit.project.engine.struct.StructSimple;

public class StructSimpleImpl extends StructImpl implements StructSimple {

	String pattern_;
	String allowedChars;
	String allowedVals;
	int minLength = 0;
	int maxLength = 0;



	public StructSimpleImpl(){}
	
	public StructSimpleImpl(String name) {
		super(name);
	}


	public String getCalcSimplePattern() {
		if (pattern_ != null && !"".equals(pattern_))
			return pattern_;
		else if (allowedChars != null && !"".equals(allowedChars)){
			return "["+quote(allowedChars)+"]{" + minLength + "," + maxLength +"}";
		} else if (allowedVals != null && !"".equals(allowedVals)) {
			StringBuffer sb = new StringBuffer();
			for (String val:allowedVals.split("\n")) {
				sb.append(quote(val.trim())+"|");
			}
			return sb.substring(0, sb.length() - 1);
		}
		
		return null;
	}

	public StructSimple setSimplePattern(String pattern) {
		this.pattern_ = pattern;
		return this;
	}

	public String calcRegex(CalcContext context, String delimiter) {
		if (this.regex == null) {
			/*if (!(this.getMinOccurrences() == 1 && this.getMaxOccurrences() == 1)) {
				context.calcIndex++;
				if (getParentDelimiter() != null) {
					context.calcIndex+=1;
				}
			}*/
			this.indexWithoutOccur = context.calcIndex;
			handleOccurIndex(context, delimiter);
			this.regexIndex = context.calcIndex;
			context.calcIndex++;
			String np = getCalcSimplePattern();
			np = np.replaceAll(quote("\\("), "");
			np = np.replaceAll(quote("(?"), "");
			int length = np.length();
			length = length - np.replaceAll(quote("("), "").length();
		/*	if (length > 0) {
				System.out.println(this.parent.name);
			}*/
			context.calcIndex += length;


			//System.out.println("name=" + name + " index=" + regexIndex + " occ=" + getMinOccurrences());
			this.regexPure ="(" + getCalcSimplePattern() + ")";
			if (getPrefix() != null)
				regexPure = quote(getPrefix()) + regexPure;
			if (getSuffix() != null)
				regexPure = regexPure + quote(getSuffix());

			StringBuffer res = handleOccur(new StringBuffer(this.regexPure), context, delimiter);
			this.regex = res.toString();
		}
	/*	if (delimiter != null)
			regex = regex + delimiter;*/

		return regex;
	}

	@Override
	public String getAllowedChars() {
		return allowedChars;
	}

	@Override
	public StructSimple setAllowedChars(String chars) {
		this.allowedChars = chars;
		return this;
	}

	@Override
	public int getMinLength() {
		return minLength;
	}

	@Override
	public StructSimple setMinLength(int length) {
		minLength = length;
		return this;
	}

	@Override
	public int getMaxLength() {		
		return maxLength;
	}

	@Override
	public StructSimple setMaxLength(int length) {
		maxLength = length;
		return this;
	}

	@Override
	public String getAllowedValues() {
		return allowedVals;
	}

	@Override
	public StructSimple setAllowedValues(String vals) {
		this.allowedVals = vals;
		return this;
	}

	@Override
	public String getSimplePattern() {
		return pattern_;
	}

}
